<?php
namespace App\Models\Chain\InviteCash;



use App\Constants\CashConstant;
use App\Models\Factory\CashFactory;
use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Orm\AwUserAccountLog;
use App\Models\Orm\AwUserInviteCash;
use App\Models\Orm\User;

/**
 * 更新现金账户表
 * Class CreateCreditLogAction
 * @package App\Models\Chain\Identity
 */

class UpdateUserAccountAction extends AbstractHandler
{
    private $params = array();
    protected $error = array('error' => '更新现金账户表失败', 'code' => 7002);
    private $user = null;


    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handleRequest()
    {
        if ($this->updateUserAccount($this->params) == true) {
            return true;
        } else {
            return $this->error;
        }
    }


   //更新用户现金表
    public function updateUserAccount($params)
    {
        $user = User::where('id',$params['user_id_invite'])->first();
        // 用户可提现余额
        $account = $user->getAccount();
        $balance = $account->balance;
        // 生成资金记录
        $insert_data = [
            'user_id' => $params['user_id_invite'],
            'action' => AwUserAccountLog::ACTION_INVITE,
            'order_id' => $params['cash_id'],
            'type' => AwUserAccountLog::TYPE_ADD,
            'status' => AwUserAccountLog::STATUS_ACCESS,
            'money' => AwUserInviteCash::INVITE_CASH,
            'balance' => $balance + AwUserInviteCash::INVITE_CASH,
        ];

        $account_log_id = AwUserAccountLog::insertGetId($insert_data);

        $account->balance += AwUserInviteCash::INVITE_CASH;

        return $account -> save();

    }


}